<template>
  <el-card class="box-card">
    <el-row
      slot="header"
      class="clearfix title"
      type="flex"
      justify="space-between"
    >
      <el-col style="font-size:16px;"><strong>创建考试</strong></el-col>
      <el-col>
        <el-button
          style="float: right"
          type="primary"
          plain
          size="small"
          @click="$emit('back')"
        >返回列表</el-button>
      </el-col>
    </el-row>
    <!-- 新增表单 -->
    <el-form
      ref="form"
      style="padding-left: 200px"
      label-position="left"
      :rules="formRules"
      :model="form"
      label-width="120px"
    >
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="考试名称" required prop="exam_name">
            <el-input
              v-model.trim="form.exam_name"
              placeholder="请输入考试名称"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="考试类型" prop="exam_type">
            <el-radio-group v-model="form.exam_type" prop="resource">
              <el-radio label="1">阶段测试</el-radio>
              <el-radio label="2">综合考试</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12" :offset="0">
          <el-form-item label="锁屏方式" prop="lock_screen">
            <el-radio-group v-model="form.lock_screen" prop="resource">
              <el-radio label="1">不锁屏</el-radio>
              <el-radio label="2">锁屏</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="归属学科" prop="subject_id">
          <el-select
            v-model="form.subject_id"
            placeholder="归属学科"
            clearable
            @change="subject"
          >
            <el-option
              v-for="item in data"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="归属目录" prop="directory_id">
          <el-select
            v-model="form.directory_id"
            placeholder="归属目录"
            clearable
            @change="dsb"
          >
            <el-option
              v-for="item in list3"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="考试时长" prop="exam_time">
          <el-input-number
            v-model="form.exam_time"
            :min="1"
            label="描述文字"
          />
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item label="试卷信息" prop="main_paper_id">
          <el-select
            v-model="form.main_paper_id"
            placeholder="选择主考试卷"
            clearable
            :disabled="flag"
          >
            <el-option
              v-for="item in list4"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-row>
      <el-row>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">立即创建</el-button>
          <el-button @click="reset">取消</el-button>
        </el-form-item>
      </el-row>
    </el-form>
  </el-card>
</template>

<script>
import { addExam } from '@/api/exam/exam'
export default {
  name: 'AddExam',
  props: {
    data: { // 存储归属学科下拉框数据
      type: Array, // 类型
      required: true // 是否必须
    }

  },
  data() {
    return {

      list3: [], // 存储归属目录下拉框数据
      list4: [], // 存储试卷信息下拉框数据

      flag: true, // 判断试卷信息下拉框是否禁用
      form: {
        directory_id: '', // 存储归属目录下拉框的值
        exam_name: '',
        exam_time: '30', // 存储考试时长
        exam_type: '1',
        lock_screen: '1',
        main_paper_id: '', // 存储试卷信息下拉框的值
        subject_id: ''// 存储归属学科下拉框的值
      },
      paperList: [], // 表格里的考试信息
      treeList: [],
      // 表单验证规则
      formRules: {
        directory_id: [{ required: true, message: '请输入', trigger: 'blur' }],
        exam_name: [{ required: true, message: '请输入', trigger: 'blur' }],
        exam_time: [{ required: true, message: '请输入', trigger: 'blur' }],
        exam_type: [{ required: true, message: '请输入', trigger: 'blur' }],
        lock_screen: [{ required: true, message: '请输入', trigger: 'blur' }],
        main_paper_id: [{ required: true, message: '请输入', trigger: 'blur' }],
        subject_id: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    }
  },

  methods: {
    // 渲染归属目录
    subject() {
      console.log()
      if (this.form.subject_id) {
        this.data.forEach(item => {
          if (item.id === this.form.subject_id) { this.list3 = item.children }
        })
      }
    },

    // 判断试卷信息下拉框是否禁用 然后渲染归属目录
    dsb() {
      if (this.form.directory_id) { // 判断上一个框有值 就解除禁用
        this.flag = false// 判断上一个框有值 就解除禁用

        // 遍历上一个框的数据组 把对应的子数据赋值给准备好的数组
        this.list3.forEach(item => {
          if (item.id === this.form.directory_id) { this.list4 = item.children }
        })
      }
    },

    // 立即创建 兜底检验
    onSubmit() {
      this.$refs.form.validate(async(valid) => {
        if (valid) {
          await addExam(this.form)
          this.$message.success('新增成功')
          this.$emit('initData')
          // this.$emit('back')
        } else {
          this.$message.error('校验失败')
          return false
        }
      })
    },
    // 清空表单内容
    reset() {
      this.form = {
        directory_id: '', // 存储归属目录下拉框的值
        exam_name: '',
        exam_time: '30', // 存储考试时长
        exam_type: '1',
        lock_screen: '1',
        main_paper_id: '', // 存储试卷信息下拉框的值
        subject_id: ''// 存储归属学科下拉框的值
      }
      this.$refs.form.resetFields()
    }

  }
}

</script>

<style lang="scss" scoped></style>
